<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
    <title>axios 发送 AJAX请求</title>
</head>
<body>
    <button>GET</button>
    <button>POST</button>
    <button>AJAX</button>
</body>
<script>
    const btns = document.querySelectorAll('button');

    //配置 baseURL
    axios.defaults.baseURL = 'http://127.0.0.1:8000';
    btns[0].onclick = function(){
        axios.get('/axios-server',{
            params:{
                id:100,
                ip:6
            },
            Headers:{
                name:'轨迹',
                age:21
            }
        }).then(value =>{
            console.log(value);
        })
    }
    btns[1].onclick = function () {
        //axios.post(url,data,[,config])第一个参数为url，第二个默认为请求体
            axios.post('/axios-server', {
                username: 'admin',
                password: 'admin'
            }, {
                //url 参数
                params: {
                    id: 200,
                    vip: 9
                },
                //请求头参数
                headers: {
                    height: 180,
                    weight: 180,
                }
            });
        }
        btns[2].onclick = function(){
            axios({
                method:'POST',
                url:'/axios-server',
                params:{
                    vip:10,
                    level:30
                },
                headers:{
                    a:100,
                    b:200
                },
                data:{
                    username:'轨迹',
                    password:'123456'
                }
            }).then(response=>{
                console.log(response.status);
                console.log(response.statusText);
                console.log(response.headers);
                console.log(response.data);
            })
        }
</script>
</html>